package com.lmz.project.redis.arithmetic.threadLearn;

import java.util.concurrent.*;

public class ThreadPool {
    public static void main(String[] args) {
        //1.提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
        //设置线程池属性
//        service1.setCorePoolSize(15);
//        service1.setKeepAliveTime();
        //2.执行指定的线程的操作，需要提供实现Runnable接口或者Callable接口实现类的对象
        RunnableTest runnableTest1 = new RunnableTest();
        RunnableTest runnableTest2 = new RunnableTest();
        service.execute(runnableTest1); //runnable方式
        service.execute(runnableTest2); //runnable方式
        CallableTest callableTest = new CallableTest();
        FutureTask<Integer> futureTask = new FutureTask<>(callableTest);
        service.submit(futureTask); //callable方式
        try {
            Integer sum = futureTask.get();
            System.out.println("总数："+sum);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        //关闭线程池
        service.shutdown();

    }
}
